import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import scipy.io as io
from sklearn import metrics
#%%  导入训练集和测试集
xtrain=io.loadmat('xtrain.mat');    xtrain=xtrain['inbase']
ytrain=io.loadmat('ytrain.mat');    ytrain=ytrain['outbase']
xtest=io.loadmat('xtest');          xtest=xtest['P_test']
ytest=io.loadmat('ytest');          ytest=ytest['T_test']
normal=io.loadmat('normal');

ytraint=ytrain[:,0];    ytestt=ytest[:,0]   #故障类型
ytrainl=ytrain[:,1];    ytestl=ytest[:,1]   #故障等级

# 加载模型
model=tf.keras.models.load_model('nn_reg_model.h5')

#%%
ypre=model.predict(xtest)

title=['Radial outward twist','Radial inward twist','Axial displacement',
       'Metal foreign body','Broken coil','short circuit between turns']
plt.figure(figsize=(14,7))
ax1=plt.gca()
ax1.set_title('consequence')
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.plot(ytestl[5*i:5*i+5],'-*b',label='real',lw=3)
    plt.plot(ypre[5*i:5*i+5],'-*y',label='predict',lw=3)
    plt.title(title[i])
    plt.xticks(())
    plt.yticks([0,10,20,30],[0,10,20,30])
    plt.xlabel('samples');plt.ylabel('fault level')
    ax=plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_color('none')
    plt.ylim=((0,30,10))
    plt.legend(loc='lower center')
#%% 做表
ytestl=ytestl.reshape(-1,1)
data=ypre-ytestl
data=abs(data)
for i in range(6):
    print('第{}个故障最大值：{}'.format(i+1,max(data[5*i:5*i+5])))
    print('第{}个故障最小值：{}'.format(i+1,min(data[5*i:5*i+5])))
    print('第{}个故障最小值：{}'.format(i+1,metrics.mean_absolute_error(ytestl[5*i:5*i+5],ypre[5*i:5*i+5])))
    print('第{}个故障最小值：{}'.format(i+1,metrics.mean_squared_error(ytestl[5*i:5*i+5],ypre[5*i:5*i+5])))
    print('\n')
    
    